package com.csair.youth.dao.impl.hibernate3;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csair.youth.constant.Constant;
import com.csair.youth.database.Resource;
import com.csair.youth.database.User;
import common.util.PagingAndSorting;

/**
 * Impl:auto created by daobuilder1.1.1 
 * @version 1.0
 * @author daobuilder@csair.com
 */
public class ResourceDaoImpl extends com.csair.youth.dao.impl.hibernate3.base.ResourceDaoImplBase{
	private static final String HQL_GET_RESOURCE_BY_PATH = "from "+Resource.REF+" a where a."+Resource.PROP_ABSOLUTE_PATH+"=:path";
	private static final String HQL_GET_RESOURCE_WITH_USER = "from "+Resource.REF+" a,"+User.REF+" b where a."+Resource.PROP_UPLOAD_USER_ID+"=b."+User.PROP_ID+" and a."+Resource.PROP_ID+"=:id";
	private static final String HQL_GET_RESOURCE_AVAILABLE = "from "+Resource.REF+" a where a."+Resource.PROP_AUDIT_RESULT+"="+Constant.RESOURCE_AUDIT_PASS;
	
	public Resource getResourceByPath(String path){
		Map param = new HashMap();
		param.put("path", path);
		return this.findFirst(this.getGenericDao().find(HQL_GET_RESOURCE_BY_PATH, param, null));
	}
	
	public Resource getResourceWithUser(String id){
		Map param = new HashMap();
		param.put("id", id);
		List ret = this.getGenericDao().find(HQL_GET_RESOURCE_WITH_USER, param, null);
		if(ret == null || ret.size() == 0){
			return null;
		}
		Object[] objs = (Object[])ret.get(0);
		Resource r = (Resource)objs[0];
		User u = (User)objs[1];
		r.setUploadUserName(u.getName());
		return r;
	}
	
	public List<Resource> getLatestResources(int count){
		if(count <= 0){
			count = 10;
		}
		PagingAndSorting ps = new PagingAndSorting(0, count, "a."+Resource.PROP_AUDIT_TIME, true);
		return (List<Resource>)this.getGenericDao().find(HQL_GET_RESOURCE_AVAILABLE, ps);
	}
	
	public List<Resource> getHottestResources(int count){
		if(count <= 0){
			count = 10;
		}
		PagingAndSorting ps = new PagingAndSorting(0, count, "a."+Resource.PROP_DOWNLOAD_TIMES, true);
		return (List<Resource>)this.getGenericDao().find(HQL_GET_RESOURCE_AVAILABLE, ps);
	}
}